//
//  MKAPIHttpTestCase.m
//  MKAPITests
//
//  Created by 苏尚进 on 2022/9/5.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKAPIHttpTestCase.h"
#import "MKAPITestHttpAPIBase.h"
#import "MKAPITestDownloadImage.h"
#import "MKAPITestGetJson.h"
#import "MKAPITestPostData.h"
#import "MKAPITestForm.h"
@implementation MKAPIHttpTestCase
- (void)test_postForm0{
    MKAPITestForm *a1 = [[MKAPITestForm alloc] init];
    a1.postParam = [[MKAPIDictionary alloc] init];
    a1.postParam
        .P(@"name", @"abc")
        .P(@"topping", @[@"1",@"2"])
    ;
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_postForm"];
    [expectations addObject:e1];
    [expectations addObject:[a1 createProgressExpectationsWithTestCase:self]];
    [a1 addCompletionHandler:^(MKAPITestPostData * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if(success && [task.postResult[@"name"][0] isEqualToString:@"abc"] && [@[@"1",@"2"] isEqualToArray:task.postResult[@"topping[]"]]){
            [e1 fulfill];
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
    
}
- (void)test_postData0{
    MKAPITestPostData *a1 = [[MKAPITestPostData alloc] init];
    a1.postParam = [[MKAPIDictionary alloc] init];
    a1.postParam.P(@"name", @"abc");
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_postData"];
    [expectations addObject:e1];
    [expectations addObject:[a1 createProgressExpectationsWithTestCase:self]];
    [a1 addCompletionHandler:^(MKAPITestPostData * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if([task.postResult[@"name"] isEqual:@"abc"]){
            MKAPITestGetJson *a2 = [MKAPITestGetJson new];
            [a2 addCompletionHandler:^(__kindof MKAPITask * _Nonnull task, BOOL success, NSError * _Nullable error) {
                [e1 fulfill];
            }];
            [a2 run];
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}
- (void)test_getJson{
    MKAPITestGetJson *a1 = [[MKAPITestGetJson alloc] init];
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_getJson"];
    [expectations addObject:e1];
    [expectations addObject:[a1 createProgressExpectationsWithTestCase:self]];
    [a1 addCompletionHandler:^(MKAPITestGetJson * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if([[task.jsonValue valueForKeyPath:@"slideshow.author"] isEqual:@"Yours Truly"]){
            [e1 fulfill];
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}
- (void)test_downloadImage{
    MKAPITestDownloadImage *a1 = [[MKAPITestDownloadImage alloc] init];
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_downloadImage"];
    [expectations addObject:e1];
    [expectations addObject:[a1 createProgressExpectationsWithTestCase:self]];
    [a1 addCompletionHandler:^(MKAPITestDownloadImage * _Nonnull task, BOOL success, NSError * _Nullable error) {
        UIImage *img = task.image;
        if(img&&CGSizeEqualToSize(img.size, CGSizeMake(239, 178))){
            [e1 fulfill];
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}
- (void)test_postData{
    MKAPITestPostData *a1 = [[MKAPITestPostData alloc] init];
    a1.postParam = [[MKAPIDictionary alloc] init];
    a1.postParam.P(@"name", @"abc");
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_postData"];
    [expectations addObject:e1];
    [expectations addObject:[a1 createProgressExpectationsWithTestCase:self]];
    [a1 addCompletionHandler:^(MKAPITestPostData * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if([task.postResult[@"name"] isEqual:@"abc"]){
            [e1 fulfill];
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}


- (void)test_getJson1{
    MKAPITestGetJson *a1 = [[MKAPITestGetJson alloc] init];
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_getJson"];
    [expectations addObject:e1];
    [expectations addObject:[a1 createProgressExpectationsWithTestCase:self]];
    [a1 addCompletionHandler:^(MKAPITestGetJson * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if([[task.jsonValue valueForKeyPath:@"slideshow.author"] isEqual:@"Yours Truly"]){
            [e1 fulfill];
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}
- (void)test_downloadImage2{
    MKAPITestDownloadImage *a1 = [[MKAPITestDownloadImage alloc] init];
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_downloadImage"];
    [expectations addObject:e1];
    [expectations addObject:[a1 createProgressExpectationsWithTestCase:self]];
    [a1 addCompletionHandler:^(MKAPITestDownloadImage * _Nonnull task, BOOL success, NSError * _Nullable error) {
        UIImage *img = task.image;
        if(img&&CGSizeEqualToSize(img.size, CGSizeMake(239, 178))){
            [e1 fulfill];
        }else{
            NSLog(@"test_downloadImage2 error:%@,img:%@",error,img);
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}
- (void)test_postData3{
    MKAPITestPostData *a1 = [[MKAPITestPostData alloc] init];
    a1.postParam = [[MKAPIDictionary alloc] init];
    a1.postParam.P(@"name", @"abc");
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_postData"];
    [expectations addObject:e1];
    [expectations addObject:[a1 createProgressExpectationsWithTestCase:self]];
    [a1 addCompletionHandler:^(MKAPITestPostData * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if([task.postResult[@"name"] isEqual:@"abc"]){
            [e1 fulfill];
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}

- (void)test_wrongSSLCerts{
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_postData"];
    [expectations addObject:e1];
    MKAPIHttpRequestAFNetworking *a1 = [[MKAPIHttpRequestAFNetworking alloc] init];
    MKAPIRequestParamJSON *param = [[MKAPIRequestParamJSON alloc] init];
    param.url = @"https://httpbingo.org/json";
    a1.requestParam = param;
    
    a1.validateServerHttpsCerts = YES;
    a1.serverHttpsCerts = @[
        @"-----BEGIN CERTIFICATE-----\
        MIIETjCCAzagAwIBAgINAe5fFp3/lzUrZGXWajANBgkqhkiG9w0BAQsFADBXMQsw\
        CQYDVQQGEwJCRTEZMBcGA1UEChMQR2xvYmFsU2lnbiBudi1zYTEQMA4GA1UECxMH\
        Um9vdCBDQTEbMBkGA1UEAxMSR2xvYmFsU2lnbiBSb290IENBMB4XDTE4MDkxOTAw\
        MDAwMFoXDTI4MDEyODEyMDAwMFowTDEgMB4GA1UECxMXR2xvYmFsU2lnbiBSb290\
        IENBIC0gUjMxEzARBgNVBAoTCkdsb2JhbFNpZ24xEzARBgNVBAMTCkdsb2JhbFNp\
        Z24wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDMJXaQeQZ4Ihb1wIO2\
        hMoonv0FdhHFrYhy/EYCQ8eyip0EXyTLLkvhYIJG4VKrDIFHcGzdZNHr9SyjD4I9\
        DCuul9e2FIYQebs7E4B3jAjhSdJqYi8fXvqWaN+JJ5U4nwbXPsnLJlkNc96wyOkm\
        DoMVxu9bi9IEYMpJpij2aTv2y8gokeWdimFXN6x0FNx04Druci8unPvQu7/1PQDh\
        BjPogiuuU6Y6FnOM3UEOIDrAtKeh6bJPkC4yYOlXy7kEkmho5TgmYHWyn3f/kRTv\
        riBJ/K1AFUjRAjFhGV64l++td7dkmnq/X8ET75ti+w1s4FRpFqkD2m7pg5NxdsZp\
        hYIXAgMBAAGjggEiMIIBHjAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/BAUwAwEB\
        /zAdBgNVHQ4EFgQUj/BLf6guRSSuTVD6Y5qL3uLdG7wwHwYDVR0jBBgwFoAUYHtm\
        GkUNl8qJUC99BM00qP/8/UswPQYIKwYBBQUHAQEEMTAvMC0GCCsGAQUFBzABhiFo\
        dHRwOi8vb2NzcC5nbG9iYWxzaWduLmNvbS9yb290cjEwMwYDVR0fBCwwKjAooCag\
        JIYiaHR0cDovL2NybC5nbG9iYWxzaWduLmNvbS9yb290LmNybDBHBgNVHSAEQDA+\
        MDwGBFUdIAAwNDAyBggrBgEFBQcCARYmaHR0cHM6Ly93d3cuZ2xvYmFsc2lnbi5j\
        b20vcmVwb3NpdG9yeS8wDQYJKoZIhvcNAQELBQADggEBACNw6c/ivvVZrpRCb8RD\
        M6rNPzq5ZBfyYgZLSPFAiAYXof6r0V88xjPy847dHx0+zBpgmYILrMf8fpqHKqV9\
        D6ZX7qw7aoXW3r1AY/itpsiIsBL89kHfDwmXHjjqU5++BfQ+6tOfUBJ2vgmLwgtI\
        fR4uUfaNU9OrH0Abio7tfftPeVZwXwzTjhuzp3ANNyuXlava4BJrHEDOxcd+7cJi\
        WOx37XMiwor1hkOIreoTbv3Y/kIvuX1erRjvlJDKPSerJpSZdcfL03v3ykzTr1Eh\
        kluEfSufFT90y1HonoMOFm8b50bOI7355KKL0jlrqnkckSziYSQtjipIcJDEHsXo\
        4HA=\
        -----END CERTIFICATE-----".mkapi_secPemData//Subject: OU=GlobalSign Root CA - R3, O=GlobalSign, CN=GlobalSign
    ];
    [a1 addCompletionHandler:^(MKAPITestPostData * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if(error && error.mkapi_isCertValidError){
            SecTrustRef serverTrust = (__bridge SecTrustRef)(error.userInfo[NSURLErrorFailingURLPeerTrustErrorKey]);
            NSString *url = error.mkapi_url;
            if(serverTrust!=NULL && url.length!=0){
                NSString *serverTrustDesc = [NSString mkapi_stringWithSecTrustRef:serverTrust];
                NSLog(@"serverTrust:%@",serverTrustDesc);
                [e1 fulfill];
            }
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}
- (void)test_wrongSSLCerts_Empty{
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_postData"];
    [expectations addObject:e1];
    MKAPIHttpRequestAFNetworking *a1 = [[MKAPIHttpRequestAFNetworking alloc] init];
    MKAPIRequestParam *param = [[MKAPIRequestParam alloc] init];
    param.url = @"https://httpbingo.org/json";
    a1.requestParam = param;
    
    a1.validateServerHttpsCerts = YES;
    a1.serverHttpsCerts = nil;
    [a1 addCompletionHandler:^(MKAPITestPostData * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if(error && error.mkapi_isCertValidError){
            SecTrustRef serverTrust = (__bridge SecTrustRef)(error.userInfo[NSURLErrorFailingURLPeerTrustErrorKey]);
            NSString *url = error.mkapi_url;
            if(serverTrust!=NULL && url.length!=0){
                NSString *serverTrustDesc = [NSString mkapi_stringWithSecTrustRef:serverTrust];
                NSLog(@"serverTrust:%@",serverTrustDesc);
                [e1 fulfill];
            }
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}
- (void)test_IPRequest{
    NSMutableArray<XCTestExpectation *> *expectations = [[NSMutableArray alloc] init];
    XCTestExpectation *e1 = [self expectationWithDescription:@"test_postData"];
    [expectations addObject:e1];
    MKAPIHttpRequestAFNetworking *a1 = [[MKAPIHttpRequestAFNetworking alloc] init];
    MKAPIRequestParam *param = [[MKAPIRequestParam alloc] init];
    param.url = @"http://77.83.142.42/json";
    a1.requestParam = param;
    
    a1.validateServerHttpsCerts = NO;
    a1.serverHttpsCerts = nil;
    [a1 addCompletionHandler:^(MKAPITestPostData * _Nonnull task, BOOL success, NSError * _Nullable error) {
        if(!error){
            [e1 fulfill];
        }
    }];
    [a1 run];
    [self waitForExpectations:expectations timeout:50];
}
@end
